package com.woniuxy.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.DTO.ChangePasswordDTO;
import com.woniuxy.entity.DTO.CourierDTO;
import com.woniuxy.entity.DTO.DeliveryOrderDTO;
import com.woniuxy.entity.DTO.PerformanceDTO;
import com.woniuxy.mapper.CourierMapper;
import com.woniuxy.service.CourierService;
import com.woniuxy.util.JWTUtil;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CourierServiceImpl implements CourierService {

    @Resource
    private CourierMapper courierMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<CourierDTO> queryCourierDTO() {
        return courierMapper.queryCourierDTO();
    }

    @Override
    public Integer updateCourierById(Integer id, Integer warehouseId) {
        return courierMapper.updateCourierById(id,warehouseId);
    }

    @Override
    public List<CourierDTO> queryCourierDTOByWarehouseId(Integer warehouseId) {
        return courierMapper.queryCourierDTOByWarehouseId(warehouseId);
    }

    @Override
    public List<CourierDTO> queryRiderByName(String riderName,Integer warehouseId) {
        return courierMapper.queryRiderByName(riderName,warehouseId);
    }

    @Override
    public void riderUnbind(Integer id) {
        courierMapper.riderUnbind(id);
    }

    @Override
    public boolean queryCourierByAccount(String courierAccount) {
        return courierMapper.queryCourierByAccount(courierAccount);
    }

    @Override
    public boolean register(CourierDTO courierDTO) {
        boolean b = courierMapper.queryCourierByAccount(courierDTO.getCourierAccount());
        if(b){
            return false;
        }
        String password = SecureUtil.md5(courierDTO.getCourierPassword() + "1a2b3c");
        courierDTO.setCourierPassword(password);
        return courierMapper.register(courierDTO);
    }

    @Override
    public ResponseData login(CourierDTO courierDTO) throws JOSEException {
        // 参数校验
        if (courierDTO == null || courierDTO.getCourierAccount() == null || courierDTO.getCourierPassword() == null) {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
        String courierAccount = courierDTO.getCourierAccount();
        String md5Password = SecureUtil.md5(courierDTO.getCourierPassword() + "1a2b3c");
        CourierDTO userInfo = courierMapper.queryPasswordByAccount(courierAccount);
        if (userInfo.getCourierPassword() ==  null) {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
        if (userInfo.getCourierPassword().equals(md5Password)) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", userInfo.getId());
            hashMap.put("account", userInfo.getCourierAccount());
            String jwt = JWTUtil.createJWT(hashMap);
            stringRedisTemplate.opsForValue().set("courierJWT:" + userInfo.getId(), jwt, 60000, TimeUnit.SECONDS);
            return ResponseData.success(jwt);
        } else {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
    }

    @Override
    public CourierDTO queryProfileById(Integer userId) {
        return courierMapper.queryProfileById(userId);
    }

    @Override
    public boolean updatePasswordById(ChangePasswordDTO changePasswordDTO) {
        System.out.println(changePasswordDTO);
        if(changePasswordDTO.getAccount() == null){
            return false;
        }
        CourierDTO courierDTO = courierMapper.queryProfileById(changePasswordDTO.getId());
        String s = SecureUtil.md5(changePasswordDTO.getOldPassword() + "1a2b3c");
        if(s.equals(courierDTO.getCourierPassword())){
            String s1 = SecureUtil.md5(changePasswordDTO.getNewPassword() + "1a2b3c");
            return courierMapper.updatePasswordById(changePasswordDTO.getId(),s1);
        }
        return false;
    }

    @Override
    public List<DeliveryOrderDTO> queryAvailableOrders(Integer warehouseId) {
        return courierMapper.queryAvailableOrders(warehouseId);
    }

    @Override
    public Integer receiveOrder(List<String> orderIds, Integer courierId) {
        for (String orderId : orderIds) {
            Integer integer = courierMapper.receiveOrder(orderId, courierId);
        }
        return 1;
    }

    @Override
    public List<DeliveryOrderDTO> queryMyOrders(Integer courierId) {
        return courierMapper.queryMyOrders(courierId);
    }

    @Override
    public boolean completeOrder(String orderId) {
        System.out.println("!11");
        return courierMapper.completeOrder(orderId);
    }

    @Override
    public List<PerformanceDTO> queryPerformance(Integer userId) {
        return courierMapper.queryPerformance(userId);
    }

    @Override
    public List<PerformanceDTO> queryMonthPerformance(Integer userId) {
        return courierMapper.queryMonthPerformance(userId);
    }

    @Override
    public Integer insertCourierInfo(String orderId, Integer warehouseId) {
        return courierMapper.insertCourierInfo(orderId,warehouseId);
    }

    @Override
    public void imgLoad(Integer id, String imgSrc) {
        courierMapper.imgLoad(id,imgSrc);
    }

}
